from utils.electrode_ctrl import ElectrodeCtrl
import numpy as np
from time import sleep

def sleep_us(us:float):
    sleep(us / 1_000_000)

if __name__=='__main__':
    frame_open = np.zeros((8,8))
    elec = []
    elec.append(ElectrodeCtrl('COM13', 115200, 'com')) # 1号板子（远离风扇）
    elec.append(ElectrodeCtrl('COM14', 115200, 'com')) # 2号板子（靠近风扇）

    # 以下为压力测试代码，自行忽略
    # while True:
    #     test_freq = 100
    #     elec[0].set_tick(np.zeros((8,8)),50,100,1,500)
    #     print('*')
    #     sleep(0.5/test_freq)
    #     print(elec[0].s.read_all())
    #     sleep(0.5/test_freq)

    pattern = np.zeros((8,8))
    
    while True:
        for line in range(8):
            print(f'正在扫描第{line+1}行')

            # 计数变量
            cnt = 0
            while cnt < 1:
                
                # 对于每块板子
                for index, board in enumerate(elec):

                    # 取消板子高阻态
                    board.on()

                    # 单点，从左扫到右
                    for i in range(8):
                        pattern[line, i] = 1
                        print(f' 第{index+1}个板子{line+1}行,第{i+1}个电极点')
                        # print(pattern)
                        board.set_tick(pattern=pattern,
                            pp=300,
                            pw=100,
                            pn=3,
                            bp=2400,
                            pulse_type='neg'
                        )
                        sleep_us(1_000_000)
                        pattern[line, i] = 0

                    # pattern[line, :] = 1
                    # #print(pattern)
                    # board.set_tick(pattern=pattern,
                    #     pp=300,        #300,
                    #     pw=100,        #100,
                    #     pn=3,          #3,
                    #     bp=2400,       #2400,
                    #     pulse_type='neg'
                    # )
                    sleep_us(100_000)      #sleep_us(100_000)

                    #pattern[line, :] = 0
                    # board.set_tick(pattern=pattern, 
                    #     pp=300,    
                    #     pw=100, 
                    #     pn=3, 
                    #     bp=2400,
                    #     pulse_type='neg'
                    # )

                    board.off()

                cnt += 1
